import {setCookie, getCookie, removeCookie} from '@/utils/cookie'
import { login, logout, scanCode } from '@/api/login'
import { roleList } from '@/api/organization/role'
import { deptList } from '^/organization/department'
import { downList } from '^/organization/user'
import AesEncrypt from '@/utils/aesEncrypt'

const state = {
    token: getCookie('token') || '', 
    userName: getCookie('username') || '', //用户名
    userInfo: JSON.parse(getCookie('user') || null) || {}, //用户信息
    roles: JSON.parse(getCookie('roles') || null) || [], //权限
    avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif?imageView2/1/w/80/h/80',
    roleList: [], //角色列表
    departmentList: [], //部门列表
    userSelectList: [], //用户下拉列表
    sex: [
        {
            id: 0,
            name: "男"
        }, {
            id: 1,
            name: "女"
        }, {
            id: 2,
            name: "保密"
        }
    ],
    // 账号状态
    accountStatusList: [
        {
            id: 0,
            name: "锁定"
        }, {
            id: 1,
            name: "有效"
        }
    ],
    // 员工状态
    employeeStatusList: [
        {
            id: 1,
            name: "在职"
        }, {
            id: 2,
            name: "离职"
        }
    ],
    // 数据权限
    dataScopeStatusList: [
        {
            id: 0,
            name: "超管"
        }, {
            id: 1,
            name: "团长"
        }, {
            id: 2,
            name: "组长"
        }, {
            id: 3,
            name: "个人"
        }
    ]
}

const mutations = {
    SET_TOKEN: (state, token) => {
        state.token = token
    },
    SET_USERINFO: (state, {user, roles}) => {
        console.log(user, roles)
        state.userInfo = user
        state.roles = roles
        state.userName = user.username

        setCookie('roles', JSON.stringify(roles))
        setCookie('user', JSON.stringify(user))
        setCookie('username', user.username)
    },
    SET_ROLES: (state, data) => {
        state.roles = data
    },
    // 获取角色列表
    SET_ROLE: (state, data) => {
        state.roleList = data
    },
    // 获取部门列表列表
    SET_DEPARTMENT: (state, data) => {
        state.departmentList = data
    },
    // 获取用户下拉列表
    SET_USERDOWNLIST: (state, data) => {
        state.userSelectList = data
    },
    // 退出登录
    SET_LOGOUT: () => {
        removeCookie('token')
    }
}


const actions = {
    login ({commit}, userInfo) {
        console.log(userInfo)
        
        const {email, password} = userInfo
        console.log("加密密码", AesEncrypt.encrypt(password))
        return new Promise((resolve, reject) => {
            login({email, password: AesEncrypt.encrypt(password)}).then(res => {
                console.log(res.data)
                commit('SET_TOKEN', res.data.token)
                commit('SET_USERINFO', res.data)

                setCookie('token', res.data.token)
                resolve()
            }).catch(err => {
                // commit('SET_TOKEN', 'testToken') //临时使用，对接接口后删除
                // setToken('testToken'), //临时使用，对接接口后删除
                reject(err)
            })
        })
    },
    scanCodeLogin ({commit}, userInfo) {
        
        console.log(userInfo)
        
        const {code, state} = userInfo
        return new Promise((resolve, reject) => {
            scanCode({
                code: code,
                state: state
            }).then(res => {
                console.log(res.data)
                commit('SET_TOKEN', res.data.token)
                commit('SET_USERINFO', res.data)

                setCookie('token', res.data.token)
                resolve()
            }).catch(err => {
                // commit('SET_TOKEN', 'testToken') //临时使用，对接接口后删除
                // setToken('testToken'), //临时使用，对接接口后删除
                reject(err)
            })
        })
    },
    // 获取角色列表【用于角色下拉列表使用，也可用于角色表格】
    getRoleList ({commit}, data) {
        return new Promise((resolve, reject) => {
            roleList(data).then(res => {
                commit('SET_ROLE', res.rows)
                resolve(res)
            }).catch(err => {
                reject(err)
            })
        })
    },
    // 获取团队列表
    getDeptList ({commit}, data) {
        return new Promise((resolve, reject) => {
            deptList(data).then(res => {
                commit('SET_DEPARTMENT', res.data)
                resolve(res)
            }).catch(err => {
                reject(err)
            })
        })
    },
    // 获取用户下拉列表
    getUserDownList ({commit}, data) {
        return new Promise((resolve, reject) => {
            downList(data).then(res => {
                commit('SET_USERDOWNLIST', res.data)
                resolve(res)
            }).catch(err => {
                reject(err)
            })
        })
    },
    logout ({commit, state}) {
        return new Promise((resolve, reject) => {
            logout(state.userInfo.userId).then(res => {
                commit('SET_LOGOUT')
                resolve(res)
            }).catch(err => {
                reject(err)
            })
        })
    }
}

const getters = {

}

export default {
    namespace: true,
    state,
    mutations,
    actions,
    getters
}